$.ajax({
    type: 'POST',
    url: '/isLogin',
    async: false,
    data: {},
    dataType: 'json',
    success: function (result) {
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        if (XMLHttpRequest.status==555){
            var layer;
            layui.use(['layer', 'form'], function(){
                layer = layui.layer
                layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                    //do something
                    layer.close(index);
                    top.location.href="/login";
                });
            });
        }else{
            console.log(XMLHttpRequest.status)
            var layer;
            layui.use(['layer', 'form'], function(){
                layer = layui.layer
                layer.alert('页面加载失败',{icon:7}, function(index){
                    //do something
                    layer.close(index);
                });
            });;
        }
    },
});
var width=$('.layui-table-body').width();
layui.use('table', function(){
    // laydate.render({
    //     elem: '#demoReload'
    //     ,type:'month'
    // });
    var table = layui.table ,form = layui.form;
    //方法级渲染
    table.render({
        elem: '#LAY_table_user'
        ,url: "/system/resourceList"
        ,cols: [[
            // {field:'id', title: '序号', width:width*0.25 }
            {field:'id', title: '资源标识', width:width*0.2 }
            ,{field:'name', title: '资源名称', width:width*0.2 }
            ,{field:'permission', title: '资源权限', width:width*0.2 }
            ,{field:'url', title: '资源URL', width:width*0.2 }
            ,{fixed:'right', title:'操作', toolbar: '#barDemo', width:width*0.2 }
        ]]
        ,id: 'testReload'
        ,page: true
        ,height: 522
        ,response: {
            statusCode: 666
        }
        // ,even:true //每行条纹
        ,limits: [10,15,20,25,30]
        ,limit: 10 //每页默认显示的数量
        ,toolbar:'#toolbarDemo'  //导出打印
        ,parseData:function (res) {
            return{
                "code":res.code,
                "msg":res.msg,
                "count":res.data.count,
                "data":res.data.resourceList
            };
        }
        , done: function(res, curr, count){
        }

    });

    $('#query').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听行单击事件（单击事件为：rowDouble）
    //其中的user对应的是 table中的lay-filter="user"
    table.on('row(user)', function(obj){
        var data = obj.data;
        //标注选中样式
        obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
    });
    table.on('tool(user)', function(obj) {
        var resource = obj.data;
        $("#resourceId").val(resource.id);
        form.val('example', {
            "modify_name": resource.name
            ,"modify_permission": resource.permission
            ,"modify_url": resource.url
        });
        if(obj.event === 'modify'){
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                $('#modify_resource').css("display","block");
                layer.open({
                    type: 1,
                    title: ['资源修改', 'font-size:17px;font-weight:bolder;background-color:#796AEE;color:white;height:50px;margin-top:-2px'],
                    // skin: 'layui-layer-rim', //加上边框
                    area: ['500px', '400px'], //宽高
                    content:$('#modify_resource'),
                    end: function () {
                        $('#modify_resource').css("display","none");
                    }
                })
            });
        }
    })

});
function modifyResource() {
    var resourceId = $("#modify_resourceId").val();
    var name = $("#modify_name").val();
    var permission = $("#modify_permission").val();
    var url = $("#modify_url").val();
     if(name==null||name===""){
         layer.msg('资源名称不能为空', {icon: 2,time:2000});
    }else if(permission==null||permission===""){
         layer.msg('资源权限不能为空', {icon: 2,time:2000});
     }else if(url==null||url===""){
         layer.msg('资源URL不能为空', {icon: 2,time:2000});
     }else{
         $.ajax({
             type: 'POST',
             url: '/system/updateResource',
             async: false,
             data: {"id":resourceId,"name":name,"permission":permission,"url":url},
             dataType: 'json',
             success: function (result) {
                 if (result.code==666){
                     layer.alert('修改成功', {icon: 1},function (index) {
                         layer.closeAll();
                         $('#modify_resource').css("display","none");
                         window.location.reload()

                     });

                 }else{
                 }
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
                 if (XMLHttpRequest.status==555){
                     var layer;
                     layui.use(['layer', 'form'], function(){
                         layer = layui.layer
                         layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                             //do something
                             layer.close(index);
                             top.location.href="/login";
                         });
                     });
                 }else{
                     var layer;
                     layui.use(['layer', 'form'], function(){
                         layer = layui.layer
                         layer.alert('页面加载失败',{icon:7}, function(index){
                             layer.close(index);
                         });
                     });
                 }
             },
         });
     }

}
function resourceAddView() {
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer;
        $('#add_resource').css("display","block");
        layer.open({
            type: 1,
            title: ['资源添加', 'font-size:17px;font-weight:bolder;background-color:#796AEE;color:white;height:50px;margin-top:-2px'],
            // skin: 'layui-layer-rim', //加上边框
            area: ['500px', '400px'], //宽高
            content:$('#add_resource'),
            end: function () {
                $('#add_resource').css("display","none");
            }
        })
    });
}
function addResource() {
    var name = $("#add_name").val();
    var permission = $("#add_permission").val();
    var url = $("#add_url").val();
    if(name==null||name===""){
        layer.msg('资源名称不能为空', {icon: 2,time:2000});
    }else if(permission==null||permission===""){
        layer.msg('资源权限不能为空', {icon: 2,time:2000});
    }else if(url==null||url===""){
        layer.msg('资源URL不能为空', {icon: 2,time:2000});
    }else{
        $.ajax({
            type: 'POST',
            url: '/system/addResource',
            async: false,
            data: {"name":name,"permission":permission,"url":url},
            dataType: 'json',
            success: function (result) {
                if (result.code==666){
                    layer.alert('添加成功', {icon: 1},function (index) {
                        layer.closeAll();
                        $('#add_resource').css("display","none");
                        window.location.reload()

                    });

                }else{
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (XMLHttpRequest.status==555){
                    var layer;
                    layui.use(['layer', 'form'], function(){
                        layer = layui.layer
                        layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                            //do something
                            layer.close(index);
                            top.location.href="/login";
                        });
                    });
                }else{
                    var layer;
                    layui.use(['layer', 'form'], function(){
                        layer = layui.layer
                        layer.alert('页面加载失败',{icon:7}, function(index){
                            layer.close(index);
                        });
                    });
                }
            },
        });
    }

}